package com.mmy.fta.cargo.server.service;

import com.mmy.fta.cargo.server.dto.CarrierBlackboardPageDTO;
import com.mmy.fta.cargo.server.dto.CarrierBlackboardRequestDTO;
import com.mmy.fta.cargo.server.entity.CargoInfoEntity;
import com.mmy.fta.cargo.server.enums.CarrierBlackboardSortEnum;
import com.mmy.fta.cargo.server.mapper.CargoInfoEntityMapper;
import com.mmy.fta.usercenter.api.MembershipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author guohao.xie
 * @apiNote
 * @exportable
 * @since 2021/10/27
 */
@Service
@Slf4j
public class CarrierBlackboardPageService {
    @Autowired
    CargoInfoEntityMapper cargoInfoEntityMapper;

    @Reference
    MembershipService membershipService;

    public CarrierBlackboardPageDTO queryCargosByDriver(CarrierBlackboardRequestDTO carrierBlackboardRequestDTO) {
        if(Objects.equals(carrierBlackboardRequestDTO.getOpenId(),"")){
            return getCarrierPageInfo(carrierBlackboardRequestDTO);
        }
        boolean isColdMembership = membershipService.driverIsColdMembership(carrierBlackboardRequestDTO.getOpenId());
        if (isColdMembership) {
            return getColdCarrierPageInfo(carrierBlackboardRequestDTO);
        } else {
            return getCarrierPageInfo(carrierBlackboardRequestDTO);
        }
    }

    /**
     * 司机小黑板当天货源
     */
    public CarrierBlackboardPageDTO getCarrierPageInfo(CarrierBlackboardRequestDTO carrierBlackboardRequestDTO) {
        String sortType = CarrierBlackboardSortEnum.LOAD_TIME.getMsg();
        if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.LOAD_TIME.getCode())) {
            sortType = CarrierBlackboardSortEnum.LOAD_TIME.getMsg();
        } else if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.CARGO_FEE.getCode())) {
            sortType = CarrierBlackboardSortEnum.CARGO_FEE.getMsg();
        } else if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.MILEAGE.getCode())) {
            sortType = CarrierBlackboardSortEnum.MILEAGE.getMsg();
        }
        List<CargoInfoEntity> result = null;
        //司机小黑板运费降序，装货时间和公里数升序
        if (Objects.equals(sortType, "cargo_fee")) {
            result = cargoInfoEntityMapper.selectCarrierPageCargoFeeDesc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        } else {
            result = cargoInfoEntityMapper.selectCarrierPageAsc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        }

        CarrierBlackboardPageDTO carrierBlackboardPageDTO = new CarrierBlackboardPageDTO();
        carrierBlackboardPageDTO.setList(result);
        log.info("司机小黑板当天货源：{}", result);
        //司机小黑板当天货源总数
        int totalSize = cargoInfoEntityMapper.getTotalSize(
                carrierBlackboardRequestDTO.getLoadAddress(),
                carrierBlackboardRequestDTO.getUnloadAddress(),
                sortType,
                carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        carrierBlackboardPageDTO.setTotalSize(totalSize);
        log.info("司机小黑板当天货源总数：{}", totalSize);
        return carrierBlackboardPageDTO;
    }


    public CarrierBlackboardPageDTO getColdCarrierPageInfo(CarrierBlackboardRequestDTO carrierBlackboardRequestDTO) {
        String sortType = CarrierBlackboardSortEnum.LOAD_TIME.getMsg();
        if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.LOAD_TIME.getCode())) {
            sortType = CarrierBlackboardSortEnum.LOAD_TIME.getMsg();
        } else if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.CARGO_FEE.getCode())) {
            sortType = CarrierBlackboardSortEnum.CARGO_FEE.getMsg();
        } else if (Objects.equals(carrierBlackboardRequestDTO.getSort(), CarrierBlackboardSortEnum.MILEAGE.getCode())) {
            sortType = CarrierBlackboardSortEnum.MILEAGE.getMsg();
        }
        List<CargoInfoEntity> resultCold = null;
        //司机冷链小黑板运费降序，装货时间和公里数升序
        if (Objects.equals(sortType, "cargo_fee")) {
            resultCold = cargoInfoEntityMapper.selectColdCarrierPageDesc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        } else {
            resultCold = cargoInfoEntityMapper.selectColdCarrierPageAsc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        }


        CarrierBlackboardPageDTO carrierBlackboardPageDTO = new CarrierBlackboardPageDTO();
        carrierBlackboardPageDTO.setList(resultCold);
        log.info("冷链司机小黑板当天冷链货源：数量{}，详情{}", resultCold.size(), resultCold);

        //司机非冷链小黑板
        List<CargoInfoEntity> resultNoCold =null;
        if (Objects.equals(sortType, "cargo_fee")) {
            resultNoCold = cargoInfoEntityMapper.selectNoColdCarrierPageDesc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        } else {
            resultNoCold = cargoInfoEntityMapper.selectNoColdCarrierPageAsc(
                    carrierBlackboardRequestDTO.getLoadAddress(),
                    carrierBlackboardRequestDTO.getUnloadAddress(),
                    sortType,
                    carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        }


        if(Objects.nonNull(resultNoCold)){
            for (CargoInfoEntity cargoInfoEntity : resultNoCold) {
                carrierBlackboardPageDTO.getList().add(cargoInfoEntity);
            }
        }

        log.info("冷链司机小黑板当天非冷链货源：数量{}，详情{}", resultCold.size(), resultNoCold);


        //司机小黑板当天货源总数
        int totalSize = cargoInfoEntityMapper.getTotalSize(
                carrierBlackboardRequestDTO.getLoadAddress(),
                carrierBlackboardRequestDTO.getUnloadAddress(),
                sortType,
                carrierBlackboardRequestDTO.getPageNum() * carrierBlackboardRequestDTO.getPageSize());
        carrierBlackboardPageDTO.setTotalSize(totalSize);
        log.info("冷链司机小黑板当天货源：{}", totalSize);

        log.info("冷链司机小黑板当天货源：数量{}", carrierBlackboardPageDTO.getList().size());
        return carrierBlackboardPageDTO;
    }
}
